/**
 ******************************************************************************
 * @file           : bsp_usart_config.h
 * @brief          : USART Configuration File (STM32F4xx)
 ******************************************************************************
 * @attention
 *
 * USART硬件配置文件
 * STM32F4xx使用GPIO AF复用功能（不是AFIO重映射）
 *
 ******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __BSP_USART_CONFIG_H
#define __BSP_USART_CONFIG_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"

/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/

/* ============================================================================ */
/*                       日志系统配置（改这里的数字）                             */
/* ============================================================================ */
/* 日志后端选择：1=EasyLogger, 2=printf, 0=关闭 */
#define USART_USE_LOG     1

/* 根据配置自动定义日志宏 */
#if (USART_USE_LOG == 1)
    /* 使用EasyLogger */
    #ifndef LOG_TAG
        #define LOG_TAG    "USART"
    #endif
    #include "elog.h"
    
#elif (USART_USE_LOG == 2)
    /* 使用printf */
    #include <stdio.h>
    #define log_i(fmt, ...)     printf(fmt "\r\n", ##__VA_ARGS__)
    #define log_w(fmt, ...)     printf("[WARN] " fmt "\r\n", ##__VA_ARGS__)
    #define log_e(fmt, ...)     printf("[ERROR] " fmt "\r\n", ##__VA_ARGS__)
    #define log_d(fmt, ...)     printf("[DEBUG] " fmt "\r\n", ##__VA_ARGS__)
    
#else
    /* 关闭日志 */
    #define log_i(fmt, ...)
    #define log_w(fmt, ...)
    #define log_e(fmt, ...)
    #define log_d(fmt, ...)
#endif

/* ============================================================================ */
/*                      串口选择配置（改这里即可切换串口）                         */
/* ============================================================================ */
/**
 * @brief 串口选择（1~6）
 * 
 * 修改此值即可切换串口，引脚和外设配置会自动更改
 * 支持的串口及默认引脚（STM32F4xx）：
 *   USART1: TX=PA9,  RX=PA10  (AF7, 可选PB6/PB7)
 *   USART2: TX=PA2,  RX=PA3   (AF7, 可选PD5/PD6)
 *   USART3: TX=PB10, RX=PB11  (AF7, 可选PC10/PC11或PD8/PD9)
 *   UART4:  TX=PA0,  RX=PA1   (AF8)
 *   UART5:  TX=PC12, RX=PD2   (AF8)
 *   USART6: TX=PC6,  RX=PC7   (AF8, 可选PG14/PG9)
 */
#define USART_SELECT_NUM                1       /*!< 选择使用的串口号 */

/**
 * @brief 引脚选择配置
 * 
 * 0 = 默认引脚组1
 * 1 = 备用引脚组2
 * 
 * STM32F4xx使用GPIO AF复用，不需要AFIO重映射
 */
#define USART_PIN_SELECT                0       /*!< 引脚选择 */

/**
 * @brief 串口波特率配置
 */
#define USART_BAUDRATE                  115200  /*!< 波特率 */

/**
 * @brief 接收缓冲区大小
 */
#define USART_RX_BUF_SIZE               256     /*!< 接收缓冲区大小 */

/* ============================================================================ */
/*                    自动配置区域（请勿手动修改）                                */
/* ============================================================================ */

/* --------------------------------------------------------------------------- */
/* USART1 配置 (PA9/PA10 或 PB6/PB7)  AF7                                       */
/* --------------------------------------------------------------------------- */
#if (USART_SELECT_NUM == 1)
    #define USART_UARTx                     USART1
    #define USART_UARTx_IRQn                USART1_IRQn
    #define USART_UARTx_CLK_ENABLE()        __HAL_RCC_USART1_CLK_ENABLE()
    #define USARTx_IRQHandler               USART1_IRQHandler
    #define USART_GPIO_AF                   GPIO_AF7_USART1
    
    #if (USART_PIN_SELECT == 0)  /* PA9/PA10 */
        #define USART_TX_GPIO_PORT              GPIOA
        #define USART_TX_GPIO_PIN               GPIO_PIN_9
        #define USART_TX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOA_CLK_ENABLE()
        
        #define USART_RX_GPIO_PORT              GPIOA
        #define USART_RX_GPIO_PIN               GPIO_PIN_10
        #define USART_RX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOA_CLK_ENABLE()
    #else  /* PB6/PB7 */
        #define USART_TX_GPIO_PORT              GPIOB
        #define USART_TX_GPIO_PIN               GPIO_PIN_6
        #define USART_TX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOB_CLK_ENABLE()
        
        #define USART_RX_GPIO_PORT              GPIOB
        #define USART_RX_GPIO_PIN               GPIO_PIN_7
        #define USART_RX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOB_CLK_ENABLE()
    #endif

/* --------------------------------------------------------------------------- */
/* USART2 配置 (PA2/PA3 或 PD5/PD6)  AF7                                        */
/* --------------------------------------------------------------------------- */
#elif (USART_SELECT_NUM == 2)
    #define USART_UARTx                     USART2
    #define USART_UARTx_IRQn                USART2_IRQn
    #define USART_UARTx_CLK_ENABLE()        __HAL_RCC_USART2_CLK_ENABLE()
    #define USARTx_IRQHandler               USART2_IRQHandler
    #define USART_GPIO_AF                   GPIO_AF7_USART2
    
    #if (USART_PIN_SELECT == 0)  /* PA2/PA3 */
        #define USART_TX_GPIO_PORT              GPIOA
        #define USART_TX_GPIO_PIN               GPIO_PIN_2
        #define USART_TX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOA_CLK_ENABLE()
        
        #define USART_RX_GPIO_PORT              GPIOA
        #define USART_RX_GPIO_PIN               GPIO_PIN_3
        #define USART_RX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOA_CLK_ENABLE()
    #else  /* PD5/PD6 */
        #define USART_TX_GPIO_PORT              GPIOD
        #define USART_TX_GPIO_PIN               GPIO_PIN_5
        #define USART_TX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOD_CLK_ENABLE()
        
        #define USART_RX_GPIO_PORT              GPIOD
        #define USART_RX_GPIO_PIN               GPIO_PIN_6
        #define USART_RX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOD_CLK_ENABLE()
    #endif

/* --------------------------------------------------------------------------- */
/* USART3 配置 (PB10/PB11 或 PC10/PC11)  AF7                                    */
/* --------------------------------------------------------------------------- */
#elif (USART_SELECT_NUM == 3)
    #define USART_UARTx                     USART3
    #define USART_UARTx_IRQn                USART3_IRQn
    #define USART_UARTx_CLK_ENABLE()        __HAL_RCC_USART3_CLK_ENABLE()
    #define USARTx_IRQHandler               USART3_IRQHandler
    #define USART_GPIO_AF                   GPIO_AF7_USART3
    
    #if (USART_PIN_SELECT == 0)  /* PB10/PB11 */
        #define USART_TX_GPIO_PORT              GPIOB
        #define USART_TX_GPIO_PIN               GPIO_PIN_10
        #define USART_TX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOB_CLK_ENABLE()
        
        #define USART_RX_GPIO_PORT              GPIOB
        #define USART_RX_GPIO_PIN               GPIO_PIN_11
        #define USART_RX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOB_CLK_ENABLE()
    #else  /* PC10/PC11 */
        #define USART_TX_GPIO_PORT              GPIOC
        #define USART_TX_GPIO_PIN               GPIO_PIN_10
        #define USART_TX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOC_CLK_ENABLE()
        
        #define USART_RX_GPIO_PORT              GPIOC
        #define USART_RX_GPIO_PIN               GPIO_PIN_11
        #define USART_RX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOC_CLK_ENABLE()
    #endif

/* --------------------------------------------------------------------------- */
/* UART4 配置 (PA0/PA1)  AF8                                                   */
/* --------------------------------------------------------------------------- */
#elif (USART_SELECT_NUM == 4)
    #define USART_UARTx                     UART4
    #define USART_UARTx_IRQn                UART4_IRQn
    #define USART_UARTx_CLK_ENABLE()        __HAL_RCC_UART4_CLK_ENABLE()
    #define USARTx_IRQHandler               UART4_IRQHandler
    #define USART_GPIO_AF                   GPIO_AF8_UART4
    
    #define USART_TX_GPIO_PORT              GPIOA
    #define USART_TX_GPIO_PIN               GPIO_PIN_0
    #define USART_TX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOA_CLK_ENABLE()
    
    #define USART_RX_GPIO_PORT              GPIOA
    #define USART_RX_GPIO_PIN               GPIO_PIN_1
    #define USART_RX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOA_CLK_ENABLE()

/* --------------------------------------------------------------------------- */
/* UART5 配置 (PC12/PD2)  AF8                                                  */
/* --------------------------------------------------------------------------- */
#elif (USART_SELECT_NUM == 5)
    #define USART_UARTx                     UART5
    #define USART_UARTx_IRQn                UART5_IRQn
    #define USART_UARTx_CLK_ENABLE()        __HAL_RCC_UART5_CLK_ENABLE()
    #define USARTx_IRQHandler               UART5_IRQHandler
    #define USART_GPIO_AF                   GPIO_AF8_UART5
    
    #define USART_TX_GPIO_PORT              GPIOC
    #define USART_TX_GPIO_PIN               GPIO_PIN_12
    #define USART_TX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOC_CLK_ENABLE()
    
    #define USART_RX_GPIO_PORT              GPIOD
    #define USART_RX_GPIO_PIN               GPIO_PIN_2
    #define USART_RX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOD_CLK_ENABLE()

/* --------------------------------------------------------------------------- */
/* USART6 配置 (PC6/PC7)  AF8                                                  */
/* --------------------------------------------------------------------------- */
#elif (USART_SELECT_NUM == 6)
    #define USART_UARTx                     USART6
    #define USART_UARTx_IRQn                USART6_IRQn
    #define USART_UARTx_CLK_ENABLE()        __HAL_RCC_USART6_CLK_ENABLE()
    #define USARTx_IRQHandler               USART6_IRQHandler
    #define USART_GPIO_AF                   GPIO_AF8_USART6
    
    #define USART_TX_GPIO_PORT              GPIOC
    #define USART_TX_GPIO_PIN               GPIO_PIN_6
    #define USART_TX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOC_CLK_ENABLE()
    
    #define USART_RX_GPIO_PORT              GPIOC
    #define USART_RX_GPIO_PIN               GPIO_PIN_7
    #define USART_RX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOC_CLK_ENABLE()

#else
    #error "Invalid USART_SELECT_NUM, please select 1~6"
#endif

/* Exported macro ------------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/

#ifdef __cplusplus
}
#endif

#endif /* __BSP_USART_CONFIG_H */
